#include "StdAfx.h"
#include "kod_api.h"

#include "../FileManager/LangUtils.h"

extern bool g_bSilent;
extern CKodLibrary g_KodLib;

const int kLangIDOk     = 0x02000702;
const int kLangIDCancel = 0x02000710;

const int kLangIDPwdCaption = 0x02000B00;
const int kLangIDPwdEnter   = 0x02000B01;
const int kLangIDPwdShow    = 0x02000B02;

const int kLangIDEncodeInfo     = 0x04000003;
const int kLangIDEncodeCaption  = 0x04000004;

const int kLangIDDecodeInfo     = 0x04000001;
const int kLangIDDecodeCaption  = 0x04000005;
const int kLangIDDecodeInputPwd = 0x04000002;

CKodLibrary::CKodLibrary()
{
  _hinstDLL = LoadLibraryA(kStrKodServiceDLL);
  if (_hinstDLL) 
  {
    _procSetText = (DLLSetTextProc) GetProcAddress(_hinstDLL, kStrProcSetText);
    _procPwdGen = (DLLPwdGenProc) GetProcAddress(_hinstDLL, kStrProcPwdGen);
    _procPwdEncode = (DLLPwdEncodeProc) GetProcAddress(_hinstDLL, kStrProcPwdEncode);
    _procPwdDecode = (DLLPwdDecodeProc) GetProcAddress(_hinstDLL, kStrProcPwdDecode);
    _procPwdHash = (DLLPwdHashProc) GetProcAddress(_hinstDLL, kStrProcPwdHash);
    _procGetMsgID = (DLLGetMsgIDProc) GetProcAddress(_hinstDLL, kStrProcGetMsgID);
    _procReturnKozSucc = (DLLReturnKozRes) GetProcAddress(_hinstDLL, kStrProcReturnKozSucc);
    _procReturnKozFail = (DLLReturnKozRes) GetProcAddress(_hinstDLL, kStrProcReturnKozFail);
    _procReturnKozWarn = (DLLReturnKozRes) GetProcAddress(_hinstDLL, kStrProcReturnKozWarn);
    _procCheckKozVer = (DLLCheckKozVer) GetProcAddress(_hinstDLL, kStrProcCheckKozVer);
  }
  else
  {
    _procSetText=NULL;
    _procPwdGen=NULL;
    _procPwdEncode=NULL;
    _procPwdDecode=NULL;
    _procPwdHash=NULL;
    _procGetMsgID=NULL;
    _procReturnKozSucc=NULL;
    _procReturnKozFail=NULL;
    _procReturnKozWarn=NULL;
    _procCheckKozVer=NULL;
  }
}

CKodLibrary::~CKodLibrary()
{
  if (_hinstDLL)
    FreeLibrary(_hinstDLL);
}

void CKodLibrary::SetLocale()
{
  SetLangString((wchar_t *)kStrOk, LangString(kLangIDOk).GetBuffer(0));
  SetLangString((wchar_t *)kStrCancel, LangString(kLangIDCancel).GetBuffer(0));

  SetLangString((wchar_t *)kStrPwdCaption, LangString(kLangIDPwdCaption).GetBuffer(0));
  SetLangString((wchar_t *)kStrPwdEnter, LangString(kLangIDPwdEnter).GetBuffer(0));
  SetLangString((wchar_t *)kStrPwdShow, LangString(kLangIDPwdShow).GetBuffer(0));

  SetLangString((wchar_t *)kStrEncodeCaption, LangString(kLangIDEncodeCaption).GetBuffer(0));
  SetLangString((wchar_t *)kStrEncodeInfo, LangString(kLangIDEncodeInfo).GetBuffer(0));

  SetLangString((wchar_t *)kStrDecodeCaption, LangString(kLangIDDecodeCaption).GetBuffer(0));
  SetLangString((wchar_t *)kStrDecodeInfo, LangString(kLangIDDecodeInfo).GetBuffer(0));
  SetLangString((wchar_t *)kStrDecodeInputPwd, LangString(kLangIDDecodeInputPwd).GetBuffer(0));
}

HRESULT CKodLibrary::SetLangString(wchar_t *var, wchar_t *value)
{
  if (_procSetText) 
    return (_procSetText)(var, value);
  else
    return S_FALSE;
}

HRESULT CKodLibrary::PwdGen(wchar_t *pwd, int len)
{
  if (_procPwdGen) 
    return (_procPwdGen)(pwd, len);
  else
    return S_FALSE;
}

HRESULT CKodLibrary::PwdEncode(wchar_t *pwd, CMyProperty *prop, bool silent)
{
  if (_procPwdEncode) 
    return (_procPwdEncode)(pwd, prop, silent);
  else
    return S_FALSE;
}

HRESULT CKodLibrary::PwdDecode(wchar_t *pwd, int len, CMyProperty *prop, HWND parent, bool silent)
{
  if (_procPwdDecode) 
    return (_procPwdDecode)(pwd, len, prop, parent, silent);
  else
    return S_FALSE;
}


HRESULT CKodLibrary::PwdHash(wchar_t *pwd, wchar_t *hash, int len)
{
  if (_procPwdHash) 
    return (_procPwdHash)(pwd, hash, len);
  else
    return S_FALSE;
}

HRESULT CKodLibrary::GetMessageID()
{
  if (_procGetMsgID) 
    return (_procGetMsgID)();
  else
    return NULL;
}

HRESULT CKodLibrary::ReturnKozSucc(int handle, int id)
{
  if (_procReturnKozSucc) 
    return (_procReturnKozSucc)(handle, id);
  else
    return S_FALSE;
}

HRESULT CKodLibrary::ReturnKozFail(int handle, int id)
{
  if (_procReturnKozFail) 
    return (_procReturnKozFail)(handle, id);
  else
    return S_FALSE;
}

HRESULT CKodLibrary::ReturnKozWarn(int handle, int id)
{
  if (_procReturnKozWarn) 
    return (_procReturnKozWarn)(handle, id);
  else
    return S_FALSE;
}

HRESULT CKodLibrary::CheckKozVer(wchar_t *ver)
{
  if (_procCheckKozVer) 
    return (_procCheckKozVer)(ver);
  else
    return S_FALSE;
}

// Wrapper functions
HRESULT GenerateMyPassword(UString &pwd)
{
  int len=64;
  pwd.GetBuffer(len);
  HRESULT rel=g_KodLib.PwdGen(pwd.GetBuffer(0), len);
  pwd.ReleaseBuffer();
  return rel;
}

HRESULT EncodeMyPassword(UString &pwd, CMyProperty &prop)
{
  return g_KodLib.PwdEncode(pwd.GetBuffer(0), &prop, g_bSilent);    
}

HRESULT DecodeMyPassword(UString &pwd, CMyProperty &prop, HWND parent)
{
  int len=64;
  pwd.GetBuffer(len);  
  HRESULT rel=g_KodLib.PwdDecode(pwd.GetBuffer(0), len, &prop, parent, g_bSilent);
  pwd.ReleaseBuffer();
  return rel;
}

UString GetMyPasswordHash(UString &pwd)
{
  UString hash;
  int len=64;
  hash.GetBuffer(len);
  HRESULT rel=g_KodLib.PwdHash(pwd.GetBuffer(0), hash.GetBuffer(0), len); 
  hash.ReleaseBuffer();
  return (rel==S_OK) ? hash : L"";
}

UINT GetMyKodMessageID()
{
  return (UINT)g_KodLib.GetMessageID();
}

HRESULT ReturnMyKozSucc(int handle, int id)
{
  return g_KodLib.ReturnKozSucc(handle, id);
}

HRESULT ReturnMyKozFail(int handle, int id)
{
  return g_KodLib.ReturnKozFail(handle, id);
}

HRESULT ReturnMyKozWarn(int handle, int id)
{
  return g_KodLib.ReturnKozWarn(handle, id);
}

HRESULT CheckMyKozVer(wchar_t *ver)
{
  return g_KodLib.CheckKozVer(ver);
}